﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;


namespace Nivdal
{
    public partial class Finances : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.Finances_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            decimal ThisWeekBalance;
            decimal LastWeekBalance;
            decimal MonthlyBalance;
            decimal MonthlyFinalBalance = 0;
            //decimal MonthlyBalance;

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand("Nivdal_GetEconomicInfoForTeam", conn);
            SqlCommand cmd1 = new SqlCommand("Nivdal_GetMonthlyEconomicBalanceForTeam", conn1);
            SqlDataReader reader;
            SqlDataReader reader1;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd1.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@TeamId", SqlDbType.Decimal, 12);
            cmd.Parameters["@TeamId"].Value = ou.TeamId;
            cmd1.Parameters.Add("@TeamId", SqlDbType.Decimal, 12);
            cmd1.Parameters["@TeamId"].Value = ou.TeamId;
            cmd.Parameters.Add("@Language", SqlDbType.Decimal, 3);
            cmd.Parameters["@Language"].Value = GND.GetUserLanguage();

            try
            {
                conn.Open();
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    //if (!reader.Read())
                    //    Response.Redirect("~/Default.aspx");
                    reader.Read();
                    // Reading this week data
                    IncomeFromCrowdThisWeekLabel.Text = reader.GetDecimal(1).ToString("N0");
                    IncomeFromSponsorsThisWeekLabel.Text = reader.GetDecimal(2).ToString("N0");
                    IncomeFromSalesThisWeekLabel.Text = reader.GetDecimal(3).ToString("N0");
                    IncomeOtherThisWeekLabel.Text = reader.GetDecimal(4).ToString("N0");
                    WagesThisWeekLabel.Text = reader.GetDecimal(5).ToString("N0");
                    ArenaExpencesThisWeekLabel.Text = reader.GetDecimal(6).ToString("N0");
                    TotalPurchasesThisWeekLabel.Text = reader.GetDecimal(7).ToString("N0");
                    OtherExpencesThisWeekLabel.Text = reader.GetDecimal(8).ToString("N0");
                    TotalIncomeThisWeekLabel.Text = reader.GetDecimal(9).ToString("N0");
                    ThisWeekBalance = reader.GetDecimal(9);
                    TotalOutcomeThisWeekLabel.Text = reader.GetDecimal(10).ToString("N0");
                    ThisWeekBalance -= reader.GetDecimal(10);
                    ThisWeekInitialBalanceLabel.Text = reader.GetDecimal(11).ToString("N0");
                    ThisWeekFinalBalanceLabel.Text = reader.GetDecimal(12).ToString("N0");
                    LastMonthFinalBalanceLabel.Text = reader.GetDecimal(12).ToString("N0");
                    MonthlyFinalBalance = reader.GetDecimal(12);
                    CurrentBalanceLabel.Text = reader.GetDecimal(12).ToString("N0");
                    CurrentBalanceLabel.CssClass = "team_name " + PlusMinusFormat(reader.GetDecimal(12));
                    ThisWeekBalanceLabel.Text = ThisWeekBalance.ToString("N0");
                    ThisWeekBalanceLabel.CssClass = PlusMinusFormat(ThisWeekBalance);
                    BalanceThisWeekLabelpositive.Text = ThisWeekBalance.ToString("N0");
                    BalanceThisWeekLabelNegative.Text = ThisWeekBalance.ToString("N0");
                    BalanceThisWeekLabelpositive.Visible = (ThisWeekBalance >= 0);
                    BalanceThisWeekLabelNegative.Visible = (ThisWeekBalance < 0);

                    // reading Last week data
                    //if (!reader.Read())
                    //    Response.Redirect("~/Default.aspx");
                    reader.Read();
                    IncomeFromCrowdLastWeekLabel.Text = reader.GetDecimal(1).ToString("N0");
                    IncomeFromSponsorsLastWeekLabel.Text = reader.GetDecimal(2).ToString("N0");
                    IncomeFromSalesLastWeekLabel.Text = reader.GetDecimal(3).ToString("N0");
                    IncomeOtherLastWeekLabel.Text = reader.GetDecimal(4).ToString("N0");
                    WagesLastWeekLabel.Text = reader.GetDecimal(5).ToString("N0");
                    ArenaExpencesLastWeekLabel.Text = reader.GetDecimal(6).ToString("N0");
                    TotalPurchasesLastWeekLabel.Text = reader.GetDecimal(7).ToString("N0");
                    OtherExpencesLastWeekLabel.Text = reader.GetDecimal(8).ToString("N0");
                    TotalIncomeLastWeekLabel.Text = reader.GetDecimal(9).ToString("N0");
                    LastWeekBalance = reader.GetDecimal(9);
                    TotalOutcomeLastWeekLabel.Text = reader.GetDecimal(10).ToString("N0");
                    LastWeekBalance -= reader.GetDecimal(10);
                    LastWeekInitialBalanceLabel.Text = reader.GetDecimal(11).ToString("N0");
                    LastWeekFinalBalanceLabel.Text = reader.GetDecimal(12).ToString("N0");
                    LastWeekBalanceLabel.Text = LastWeekBalance.ToString("N0");
                    LastWeekBalanceLabel.CssClass = PlusMinusFormat(LastWeekBalance);
                    BalanceLastWeekLabelpositive.Text = LastWeekBalance.ToString("N0");
                    BalanceLastWeekLabelNegative.Text = LastWeekBalance.ToString("N0");
                    BalanceLastWeekLabelpositive.Visible = (LastWeekBalance >= 0);
                    BalanceLastWeekLabelNegative.Visible = (LastWeekBalance < 0);
                }



            }
            catch
            {
                //Response.Write("error");
            }
            finally
            {
                conn.Close();
            }

            try
            {
                conn1.Open();
                reader1 = cmd1.ExecuteReader();

                if (reader1.HasRows)
                {
                    
                    reader1.Read();
                    IncomeFromCrowdLastMonthLabel.Text = reader1.GetDecimal(0).ToString("N0");
                    IncomeFromSponsorsLastMonthLabel.Text = reader1.GetDecimal(1).ToString("N0");
                    IncomeFromSalesLastMonthLabel.Text = reader1.GetDecimal(2).ToString("N0");
                    IncomeOtherLastMonthLabel.Text = reader1.GetDecimal(3).ToString("N0");

                    WagesLastMonthLabel.Text = reader1.GetDecimal(4).ToString("N0");
                    ArenaExpencesLastMonthLabel.Text = reader1.GetDecimal(5).ToString("N0");
                    TotalPurchasesLastMonthLabel.Text = reader1.GetDecimal(6).ToString("N0");
                    OtherExpencesLastMonthLabel.Text = reader1.GetDecimal(7).ToString("N0");

                    TotalIncomeLastMonthLabel.Text = reader1.GetDecimal(8).ToString("N0");
                    MonthlyBalance = reader1.GetDecimal(8);
                    TotalOutcomeLastMonthLabel.Text = reader1.GetDecimal(9).ToString("N0");
                    MonthlyBalance -= reader1.GetDecimal(9);
                    MonthlyBalanceLabel.Text = MonthlyBalance.ToString("N0");
                    MonthlyBalanceLabel.CssClass = PlusMinusFormat(MonthlyBalance);
                    LastMonthInitialBalanceLabel.Text = (MonthlyFinalBalance - MonthlyBalance).ToString("N0");
                    BalanceLastMonthLabelpositive.Text = MonthlyBalance.ToString("N0");
                    BalanceLastMonthLabelNegative.Text = MonthlyBalance.ToString("N0");
                    BalanceLastMonthLabelpositive.Visible = (MonthlyBalance >= 0);
                    BalanceLastMonthLabelNegative.Visible = (MonthlyBalance < 0);
                }
            }
            catch
            {
                //Response.Write("error");
            }
            finally
            {
                conn1.Close();
            }

            if (!IsPostBack)
            {
                PlayerWagesdataSourse.SelectParameters.Add("UserId", ou.UserId);
                WagesGridView.UseAccessibleHeader = true;
                WagesGridView.HeaderRow.TableSection = TableRowSection.TableHeader;
            }
            
          

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        decimal TotalWages = 0;

        protected void WagesGridViewRowDataBound(object sender, GridViewRowEventArgs e)
        {
            try { TotalWages += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Wage")); }
            catch { }
        }
        protected void WagesDataBound(object sender, EventArgs e)
        {
            TotalWagesLabel.Text = TotalWages.ToString("N0");
        }

        protected string FormattedWage(object Wage)
        {
            decimal ReturnValue;

            if (decimal.TryParse(Wage.ToString(), out ReturnValue))
                return (ReturnValue.ToString("N0"));
            return "";            
        }

        protected string WageSortString(object Wage)
        {
            string Text = Wage.ToString();

            while (Text.Length < 8)
                Text = "0" + Text;

            return Text;
        }

        protected string PlusMinusFormat(decimal num)
        {
            return (num >= 0 ? "finances_plus" : "finances_minus");
        }
        
        
    }
}